package flc.ast.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.stark.calculator.tax.adapter.HeadViewHolder;
import com.stark.calculator.tax.model.WageRateBean;

import java.util.List;

import flc.ast.R;
import stark.common.basic.utils.ViewUtil;

public class MyWageRateAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEAD = 0;
    private static final int TYPE_ITEM = 1;

    private List<WageRateBean> datas;
    private boolean showPercent;

    public MyWageRateAdapter(boolean showPercent) {
        this.showPercent = showPercent;
    }

    public void setDatas(List<WageRateBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public List<WageRateBean> getDatas() {
        return datas;
    }

    @Override
    public int getItemViewType(int position) {
        int type = TYPE_ITEM;
        if (datas.get(position).getType() == WageRateBean.Type.HEAD) {
            type = TYPE_HEAD;
        }
        return type;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        if (viewType == TYPE_HEAD) {
            holder = new HeadViewHolder(LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_wage_head, parent, false));
        } else {
            holder = new ItemViewHolder(LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_wage, parent, false), showPercent);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeadViewHolder) {
            ((HeadViewHolder) holder).bindData(datas.get(position));
        } else {
            ((ItemViewHolder) holder).bindData(datas.get(position), position);
        }
    }

    @Override
    public int getItemCount() {
        if (datas != null) {
            return datas.size();
        }
        return 0;
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        private TextView tvTitle, etPersonal, etCompany;
        private View line;

        public ItemViewHolder(@NonNull View itemView, boolean showPercent) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            View viewPersonal = itemView.findViewById(R.id.include_personal);
            etPersonal = viewPersonal.findViewById(R.id.et_value);
            View viewCompany = itemView.findViewById(R.id.include_company);
            etCompany = viewCompany.findViewById(R.id.et_value);
            line = itemView.findViewById(R.id.v_line);
            if (showPercent) {
                viewPersonal.findViewById(R.id.tv_percent).setVisibility(View.VISIBLE);
                viewCompany.findViewById(R.id.tv_percent).setVisibility(View.VISIBLE);
            } else {
                viewPersonal.findViewById(R.id.tv_percent).setVisibility(View.GONE);
                viewCompany.findViewById(R.id.tv_percent).setVisibility(View.GONE);
            }
        }

        public void bindData(WageRateBean bean, int position) {
            ViewUtil.setViewText(tvTitle, bean.getName());
            ViewUtil.setViewText(etPersonal, bean.getPersonal());
            ViewUtil.setViewText(etCompany, bean.getCompany());

            if (position == getItemCount() - 1) {
                line.setVisibility(View.GONE);
            } else {
                line.setVisibility(View.VISIBLE);
            }
            //etPersonal textWatcher
            MyTextWatcher personalTextWatcher = (MyTextWatcher) etPersonal.getTag();
            if (personalTextWatcher == null) {
                personalTextWatcher = new MyTextWatcher(true);
                etPersonal.addTextChangedListener(personalTextWatcher);
                etPersonal.setTag(personalTextWatcher);
            }
            personalTextWatcher.setRateBean(bean);

            //etCompany textWatcher
            MyTextWatcher companyTextWatcher = (MyTextWatcher) etCompany.getTag();
            if (companyTextWatcher == null) {
                companyTextWatcher = new MyTextWatcher(false);
                etCompany.addTextChangedListener(companyTextWatcher);
                etCompany.setTag(companyTextWatcher);
            }
            companyTextWatcher.setRateBean(bean);
        }

        private class MyTextWatcher implements TextWatcher {

            private WageRateBean rateBean;
            private boolean isPersonal = true;

            public void setRateBean(WageRateBean rateBean) {
                this.rateBean = rateBean;
            }

            public MyTextWatcher(boolean isPersonal) {
                this.isPersonal = isPersonal;
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = s.toString().trim();
                if (rateBean != null) {
                    if (isPersonal) {
                        rateBean.setPersonal(text);
                    } else {
                        rateBean.setCompany(text);
                    }
                }
            }
        }
    }
}

